package me.pinion.model;


import com.jfinal.plugin.activerecord.TableInfoMapping;
import me.pinion.Log;
import me.pinion.model.annotation.TableBind;

import java.util.Date;
import java.util.List;

@TableBind(table = "x_visit")
public class Visit extends XModel<Visit> {
    public static Visit dao = new Visit();


    public Visit visit(XModel model, User user) {

        Visit visit = new Visit();
        try {
            visit.set("user", user.getId());
        } catch (NullPointerException e) {
            Log.e(e);
        }
        visit.set("target_id", model.getId());
        visit.set("target_type", model.getModeType().getId());
        visit.save();
        return visit;
    }

    public <T extends XModel<T>> List<T> visitRank(Class<T> clazz, User user, int max) {
        String tablename = TableInfoMapping.me().getTableInfo(clazz).getTableName();
        return ModelType.dao.getByClass(clazz).<T>getModelDAO().paginate(1, max, "SELECT \n" +
                "    so.*, count(v.id) AS visit_count, max(v.datetime) AS last_visit \n",
                "FROM\n" +
                        "    " + tablename + " as so,\n" +
                        "    x_visit AS v,\n" +
                        "\tuser AS u\n" +
                        "WHERE\n" +
                        "    so.id = v.target_id\n" +
                        "AND\n" +
                        "\tv.user=u.id\n" +
                        "\tAND u.id=?\n" +
                        "\tAND v.target_type=?\n" +
                        "GROUP BY so.id\n" +
                        "ORDER BY count(v.id) DESC", user.getId(), ModelType.dao.getByClass(clazz).getId()).getList();
    }

    public long count(XModel<? extends XModel<?>> model){
        return Visit.dao.findFirst(
                "SELECT COUNT(v.id) AS visit_count FROM x_visit AS v WHERE v.target_type=? AND v.target_id=?"
                , model.getModelId()
                , model.getId()
        ).getLong("visit_count");
    }

    public long count(XModel<? extends XModel<?>> model, Date from){
        return Visit.dao.findFirst(
                "SELECT COUNT(v.id) AS visit_count FROM x_visit AS v WHERE v.target_type=? AND v.target_id=? AND v.datetime > ?"
                , model.getModelId()
                , model.getId()
                , from
        ).getLong("visit_count");
    }

    public long count(XModel<? extends XModel<?>> model, User user){
        return Visit.dao.findFirst(
                "SELECT COUNT(v.id) AS visit_count FROM x_visit AS v WHERE v.target_type=? AND v.target_id=? AND v.user=?"
                , model.getModelId()
                , model.getId()
                , user.getId()
        ).getLong("visit_count");
    }

    public long count(XModel<? extends XModel<?>> model, User user, Date from){
        return Visit.dao.findFirst(
                "SELECT COUNT(v.id) AS visit_count FROM x_visit AS v WHERE v.target_type=? AND v.target_id=? AND v.user=? AND v.datetime > ?"
                , model.getModelId()
                , model.getId()
                , user.getId()
                , from
        ).getLong("visit_count");
    }
}
